//
//  OrderDetailViewController.swift
//  Eternity
//
//  Created by cll12_12 on 2016/12/26.
//  Copyright © 2016年 ZhengWei. All rights reserved.
//

import UIKit

class OrderDetailViewController: BaseViewController {

    var type: STATUS = .WAITFORPAY
    
    // MARK: - life cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initBaseLayout()
        layoutPageSubviews()
        configUI()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    // MAKR: - private method
    func initBaseLayout() {
        self.view.addSubview(tableView)
    }
    
    func layoutPageSubviews() {
        tableView.snp.makeConstraints { (make) in
            make.edges.equalTo(self.view).inset(UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0))
        }
    }
    
    func configUI() {
        self.view.backgroundColor = UIColor.white
        self.title = "订单详细"
        initNavItem(controller: self, imageName: "nav_back_n", highlitedImageName: "nav_back_h", selectedImageName: "", isLeft: true)
        initNavItem(controller: self, imageName: "nav_rule_n", highlitedImageName: "nav_rule_h", selectedImageName: "", isLeft: false)
    }
    
    // MARK: - setters and getters
    var _tableView: UITableView!
    var tableView: UITableView {
        if _tableView == nil {
            _tableView = UITableView(frame: CGRect.zero, style: .grouped)
            _tableView.backgroundColor = UIColor(hexString: "#EEEEEE")
            _tableView.separatorStyle = .none
            _tableView.delegate = self
            _tableView.dataSource = self
            _tableView.register(CollectionCell.self, forCellReuseIdentifier: "CollectionCell")
            _tableView.register(OrderdetailHeader.self, forHeaderFooterViewReuseIdentifier: "OrderdetailHeader")
            _tableView.register(OrderdetailFooter.self, forHeaderFooterViewReuseIdentifier: "OrderdetailFooter")
        }
        return _tableView
    }
    
    
}

extension OrderDetailViewController: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let string = "CollectionCell"
        let cell = tableView.dequeueReusableCell(withIdentifier: string, for: indexPath) as! CollectionCell
        cell.configUI()
        cell.index = 0
        cell.isOrder = true
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 128
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let string = "OrderdetailHeader"
        let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: string) as! OrderdetailHeader
        header.configUI(type: type)
        
        return header
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if self.type == .WAITFORGOODS {
            return 249
        }
        return 197
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let string = "OrderdetailFooter"
        let footer = tableView.dequeueReusableHeaderFooterView(withIdentifier: string) as! OrderdetailFooter
        footer.configUI(status: type)
        
        return footer
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 339
    }

}
